package cn.skycity.common.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import cn.skycity.common.service.ISysUserService;
import cn.skycity.framework.core.exception.ServiceException;
import com.alibaba.excel.context.AnalysisContext;
import com.skycity.common.excel.core.ExcelListener;
import com.skycity.common.excel.core.ExcelResult;
import com.skycity.framework.uaa.pojo.entity.SysUser;
import com.skycity.framework.uaa.pojo.vo.SysUserImportVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.List;

/**
 * @Description
 * @Author YingBo.Dai
 * @Date 2024/12/4
 * @Version 1.0
 */
@Slf4j
public class SysUserImportListener implements ExcelListener<SysUserImportVo>{

    private final boolean supportUpdate;

    private int successNum = 0;
    private int failureNum = 0;

    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    private final PasswordEncoder passwordEncoder;

    private final ISysUserService sysUserService;


    public SysUserImportListener(boolean supportUpdate, PasswordEncoder passwordEncoder, ISysUserService sysUserService){
        this.supportUpdate = supportUpdate;
        this.passwordEncoder = passwordEncoder;
        this.sysUserService = sysUserService;
    }

    @Override
    public ExcelResult<SysUserImportVo> getExcelResult() {
        return new ExcelResult<>() {
            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                    throw new ServiceException(failureMsg.toString());
                } else {
                    successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
                }
                return successMsg.toString();
            }

            @Override
            public List<SysUserImportVo> getList() {
                return null;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }

    @Override
    public void invoke(SysUserImportVo data, AnalysisContext context) {
        String encode = passwordEncoder.encode("123456");
        SysUser sysUser = sysUserService.getByUsername(data.getUserName());
        //用户不存在
        if(ObjUtil.isNull(sysUser)){
            SysUser bean = BeanUtil.toBean(data, SysUser.class);
            bean.setPassword(encode);
            sysUserService.save(bean);
            successNum++;
            successMsg.append("<br/>").append(successNum).append("、账号 ").append(data.getUserName()).append(" 导入成功");
        }else if(supportUpdate){
            //用户存在
            Long userId = sysUser.getId();
            SysUser user = BeanUtil.toBean(data, SysUser.class);
            user.setId(userId);
            sysUserService.updateById(user);     //更新用户信息
            successNum++;     //成功数+1     成功消息
            successMsg.append("<br/>").append(successNum).append("、账号 ").append(data.getUserName()).append(" 更新成功");
        }else{
            failureNum++;     //失败数+1     失败消息
            failureMsg.append("<br/>").append(failureNum).append("、账号 ").append(data.getUserName()).append(" 已存在，更新失败");
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }
}
